package com.gln.bloger.action;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorActionForm;

import com.gln.bloger.constant.Constant;
import com.gln.bloger.exception.AlreadyExistException;
import com.gln.bloger.pojos.User;
import com.gln.bloger.util.StringUtil;

public class UserAction extends BaseAction {
	/**
	 * add user
	 * @param mapping
	 * @param form
	 * @param reqeust
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
				return mapping.findForward("userPage");
	}
	/**
	 * edit profile
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		String id = request.getParameter("id");
		User user;
		if(!StringUtil.isBlank(id)){
			user = getUserService().getUserById(id);
		}else{
		    user = getAuthUser(request);
		}
		request.setAttribute("user", user);
		return mapping.findForward("userPage");
	}
	
	/**
	 * save user
	 * @param mapping
	 * @param form
	 * @param reqeust
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)throws Exception{
		DynaValidatorActionForm userForm = (DynaValidatorActionForm)form;
		ActionMessages errors = form.validate(mapping, request);
		String id = userForm.getString("id");
		String userName = userForm.getString("userName");
		String fullName = userForm.getString("fullName");
		String email = userForm.getString("email");
		String password = userForm.getString("password");
		String qq = userForm.getString("qq");
		String msn = userForm.getString("msn");
		User user;
		if(StringUtil.isBlank(id)){
			//register user
			user = new User();
			user.setDateCreated(new Date());
			user.setEnabled(Boolean.TRUE);
			user.setEmail(email);
			user.setUserName(userName);
			user.setPassword(password);
		}else{
			//edit profile
			user = getUserService().getUserById(id);
		}
		user.setFullName(fullName);
		user.setMsn(msn);
		user.setQq(qq);
		
		if(!errors.isEmpty()){
			saveErrors(request, errors);
			request.setAttribute("user", user);
			return mapping.getInputForward();
		}
		
		try{
			getUserService().saveOrUpdateUser(user);
		   request.getSession().setAttribute(Constant.AUTH_USER, user);
		}catch(AlreadyExistException ae){
			errors.add("errors", new ActionMessage("errors.already.exist",
					user.getEmail()));
			request.setAttribute("user", user);
			return mapping.getInputForward();
		}
		
		
		if(StringUtil.isBlank(id)){
			//register user
			//to welcome page
		    return mapping.findForward("welcome");
		}else{
			//edit profile
			return mapping.findForward("adminIndex");
		}
	}
}
